<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/27
 * Time: 15:38
 */
namespace App\Plugins\Controllers\Admin;


use App\Plugins\Forms\ShareFreeForms;

class ShareFreeController extends BaseController
{

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function index()
    {
        $form = new ShareFreeForms();

        if($this->request->ajax()) {
            $rules = [
                'page'  => 'required',
                'limit' => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui($form->getShareGoodsList($this->validated));
        }

        return view('shareFree.index', $form->getPlugin());
    }


    /**
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function order()
    {
        $form = new ShareFreeForms();

        if($this->request->ajax()) {
            $rules = [
                'page'  => 'required',
                'limit' => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui($form->getShareOrderList($this->validated));
        }

    }


    /**
     * @param $order_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function orderLog($id)
    {
        $form = new ShareFreeForms();

        if($this->request->ajax()) {
            $rules = [
                'page'  => 'required',
                'limit' => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui($form->getShareOrderLogList($this->validated, $id));
        }

        return view('shareFree.log', compact('id'));
    }


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|mixed
     * @throws \App\Exceptions\RequestException
     */
    public function editShareGoods()
    {
        $rules = [
            'id'           => 'nullable',
            'goods_id'     => 'nullable',
            'goods_name'   => 'nullable',
            'goods_image'  => 'nullable',
            'amount'       => 'nullable',
            'numbers'      => 'nullable',
            'days'         => 'nullable',
            'sort'         => 'nullable',
            'part_numbers' => 'nullable',
        ];


        $this->validateInput($rules);

        $form = new ShareFreeForms();

        if($this->request->isMethod('post')) {

            return $this->success($form->editShareGoods($this->validated));
        }

        return view('shareFree.edit_goods',  ['model' => $form->getGoodsOrNew($this->validated)]);
    }


    /**
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function deleteShareGoods()
    {
        $rules = [
            'id' => 'required',
        ];

        $this->validateInput($rules);

        return $this->success((new ShareFreeForms())->destroy($this->validated['id']));
    }
}